import Vue from 'vue'
import Router from 'vue-router'
// import plugin from './plugin';

Vue.use(Router)


const router = new Router({
    routes: [{
        path: '/',
        name: 'index',
        meta: { index: 0, requiresAuth: true },
        component: resolve => require(['@/views/index'], resolve),

    },
    {
        path: '/list',
        name: 'list',
        meta: { index: 1, requiresAuth: false },
        component: resolve => require(['@/views/list'], resolve),

    },
    {
        path: '/message',
        name: 'message',
        meta: { index: 0, requiresAuth: false },
        component: resolve => require(['@/views/message'], resolve),

    },
     {
        path: '/user/useredit/index',
        name: 'useredit',
        meta: { index: 10, requiresAuth: false },
        component: resolve => require(['@/components/user/useredit'], resolve),
    },

    {
        path: '/duban1',
        name: 'duban1',
        meta: { index: 0, requiresAuth: false },
        component: resolve => require(['@/views/duban1'], resolve),

    },{
        path: '/duban2',
        name: 'duban2',
        meta: { index: 0, requiresAuth: false },
        component: resolve => require(['@/views/duban2'], resolve),

    },{
        path: '/duban3',
        name: 'duban3',
        meta: { index: 0, requiresAuth: false },
        component: resolve => require(['@/views/duban3'], resolve),

    },{
        path: '/fuwudian1',
        name: 'fuwudian1',
        meta: { index: 0, requiresAuth: false },
        component: resolve => require(['@/views/fuwudian1'], resolve),

    },{
        path: '/fuwudian2',
        name: 'fuwudian2',
        meta: { index: 0, requiresAuth: false },
        component: resolve => require(['@/views/fuwudian2'], resolve),

    },{
        path: '/guanli1',
        name: 'guanli1',
        meta: { index: 0, requiresAuth: false },
        component: resolve => require(['@/views/guanli1'], resolve),

    },{
        path: '/guanli2',
        name: 'guanli2',
        meta: { index: 0, requiresAuth: false },
        component: resolve => require(['@/views/guanli2'], resolve),

    },{
        path: '/dianxing1',
        name: 'dianxing1',
        meta: { index: 0, requiresAuth: false },
        component: resolve => require(['@/views/dianxing1'], resolve),

    },{
        path: '/dianxing2',
        name: 'dianxing2',
        meta: { index: 0, requiresAuth: false },
        component: resolve => require(['@/views/dianxing2'], resolve),

    },
    {
        path: '/login',
        name: 'login',
        meta: { index: 0, requiresAuth: false },
        component: resolve => require(['@/views/login'], resolve),

    },

    // ...plugin
    { path: '*', meta: { index: 15 }, component: resolve => require(['@/components/common/not_found'], resolve), }
    ]
})

export default router

router.beforeEach((to, from, next) => {
    
    if (to.matched.some(record => record.meta.requiresAuth)) {

        if (!localStorage.getItem('token')) {
            next({
                path: '/login',
                query: {
                    redirect: to.fullPath
                }
            })
        } else {
            next()
        }
    } else {
        next()
    }
})